package com.qiantu.api.http;

import android.util.Log;

import com.hjq.http.EasyUtils;
import com.hjq.http.annotation.HttpIgnore;
import com.hjq.http.config.IRequestApi;
import com.qiantu.api.BuildConfig;
import com.qiantu.api.utils.Md5Util;

/**
 * @ClassName CommonApi
 * @Description TODO
 * @Author ShiJunqiang
 * @Date 2021/11/18 20:29
 * @Version 1.0
 */
public class DataApi implements IRequestApi {
    private final String id = "666";
    private final String v = "1.0";
    @HttpIgnore
    private String api;
    private String sign;
    private long timestamp;
    private Object data;
    public DataApi(String api, Object data) {
        this.api = api;
        this.data = data;
        createSign();
    }

    public DataApi(String api) {
        this.api = api;
        createSign();
    }

    @Override
    public String getApi() {
        return api;
    }

    private void createSign(){
        StringBuilder signStr = new StringBuilder(BuildConfig.SECRET);
        String dataStr = "";
        if(data != null){
//            String jsonString = JSONObject.toJSONString(data, SerializerFeature.SortField);
            String jsonString = EasyUtils.convertObject(data).toString();
            dataStr = "data" + jsonString;
            //去除\转义符号
            dataStr = dataStr.replaceAll("\\\\/", "/");
        }
        timestamp = System.currentTimeMillis();
        signStr.append(dataStr)
                .append("id" + id)
                .append("timestamp" + timestamp)
                .append("v" + v)
                .append(BuildConfig.SECRET);
        Log.d("lzx", "signStr:" + signStr);
        sign = Md5Util.getMD5ofStr(signStr.toString());
    }
}